﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using System.IO;
using ExamAnalysis.Core;
using ExamAnalysis.Model;

namespace ExamAnalysis.BasicInfo
{
    public partial class ExamItemInfo : System.Web.UI.Page
    {
        static int examitemgroupid = 0;
        static int examitemsid = 0;
        static int knowledgeid = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
                {
                    var myExamInfo = from ei in db.ExamItems
                                     join k in db.KnowledgePointInfo on ei.KnowledgePointID equals k.KnowledgePointID
                                     select new { ExamItemID = ei.ExamItemID, ItemName = ei.ItemName, ItemScore = ei.ItemScore, ItemType = ei.ItemType, DifficultyLevel = ei.DifficultyLevel, KnowledgePointName=k.KnowledgePointName };
                    this.Store2.DataSource = myExamInfo;
                    this.Store2.DataBind();

                    var myExamGroup = from eg in db.ExamItemsGroup
                                      join ex in db.ExamInfo on eg.ExamID equals ex.ExamID
                                      select new { ExamItemGroupID = eg.ExamItemGroupID, ExamName = ex.ExamName, GroupName = eg.GroupName };
                    this.Store1.DataSource=myExamGroup;
                    this.Store1.DataBind();

                    var myKnowledge = from k in db.KnowledgePointInfo
                                      select k;
                    this.Store3.DataSource = myKnowledge;
                    this.Store3.DataBind();

                    var myExamIGroup = from eg in db.ExamItemsGroup
                                       select eg;
                    this.Store4.DataSource = myExamIGroup;
                    this.Store4.DataBind();
                }
            }
        }

        protected void Button_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            RowSelectionModel sm = this.GridPanel1.GetSelectionModel() as RowSelectionModel;
            foreach (SelectedRow row in sm.SelectedRows)
            {
                examitemgroupid = Convert.ToInt32(row.RecordID);
            }
            using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
            {
                var myExamGroup = from c in db.ExamItemsGroup
                              where c.ExamItemGroupID == examitemgroupid
                              select c.GroupName;
                foreach (var group in myExamGroup)
                {
                    DropDownField1.Text = group.ToString();
                }
                var myExamInfo = from ei in db.ExamItems
                              where ei.ExamItemGroupID==examitemgroupid
                              select ei;
                this.Store2.DataSource = myExamInfo;
                this.Store2.DataBind();
            }
            examitemgroupid = 0;
        }

        protected void Button2_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            ItemScore.Text = "0";
            ItemName.Text = "";
            ItemType.Text = "";
            DifficultyLevel.Text = "0";
            DropDownField2.Text = "请选择";
            DropDownField3.Text = "请选择";
            examitemgroupid = 0;
            this.FormWindow.Visible = true;
            this.FormWindow.Show();
            Button6.Show();
            Button7.Hide();
        }

        protected void Button3_Click(object sender, Ext.Net.DirectEventArgs e)
        { 
            RowSelectionModel sm = this.ExamInfoGridPanel.GetSelectionModel() as RowSelectionModel;
            if (sm.SelectedRows.Count != 0)
            {
                foreach (SelectedRow row in sm.SelectedRows)
                {
                    examitemsid = Convert.ToInt32(row.RecordID);
                }
                using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
                {
                    var newExamItems = from ei in db.ExamItems
                                       where ei.ExamItemID == examitemsid
                                       select ei;
                    ExamAnalysis.Model.ExamItems newei = newExamItems.First();
                    var examitemgroup = from eg in db.ExamItemsGroup
                                 where eg.ExamItemGroupID== newei.ExamItemGroupID
                                 select eg.GroupName;
                    var knowledge = from k in db.KnowledgePointInfo
                                    where k.KnowledgePointID == newei.KnowledgePointID
                                    select k.KnowledgePointName;
                    examitemgroupid = newei.ExamItemGroupID;
                    knowledgeid = newei.KnowledgePointID;
                    ItemScore.Text = newei.ItemScore.ToString();
                    ItemName.Text = newei.ItemName;
                    ItemType.Text = newei.ItemType;
                    DifficultyLevel.Text = newei.DifficultyLevel.ToString();
                    DropDownField2.Text = knowledge.First().ToString();
                    DropDownField3.Text = examitemgroup.First().ToString();
                }
                this.FormWindow.Visible = true;
                this.FormWindow.Show();
                Button6.Hide();
                Button7.Show();
            }
            else
            {
                X.Msg.Alert("提示信息", "请选择一道试题").Show();
            }
        }

        protected void Button4_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            RowSelectionModel sm = this.ExamInfoGridPanel.GetSelectionModel() as RowSelectionModel;
            if (sm.SelectedRows.Count != 0)
            {
                foreach (SelectedRow row in sm.SelectedRows)
                {
                    examitemsid = Convert.ToInt32(row.RecordID);
                }
                using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
                {
                    var newexamitem = from ei in db.ExamItems
                                   where ei.ExamItemID==examitemsid
                                   select ei;
                    ExamAnalysis.Model.ExamItems newei = newexamitem.First();
                    db.ExamItems.DeleteOnSubmit(newei);
                    db.SubmitChanges();
                    X.Msg.Alert("提示信息", "删除成功").Show();
                    Response.Redirect("ExamItemsInfo.aspx");
                }
            }
            else
            {
                X.Msg.Alert("提示信息", "请选择一个试题").Show();
            }
        }

        protected void Button5_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            RowSelectionModel sm = this.GridPanel2.GetSelectionModel() as RowSelectionModel;
            foreach (SelectedRow row in sm.SelectedRows)
            {
                knowledgeid = Convert.ToInt32(row.RecordID);
            }
            using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
            {
                var myKnowledge = from k in db.KnowledgePointInfo
                              where k.KnowledgePointID == knowledgeid
                              select k.KnowledgePointName;
                foreach (var knowledge in myKnowledge)
                {
                    DropDownField2.Text = knowledge.ToString();
                }
            }
        }

        protected void Button_5_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            RowSelectionModel sm = this.GridPanel3.GetSelectionModel() as RowSelectionModel;
            foreach (SelectedRow row in sm.SelectedRows)
            {
                examitemgroupid = Convert.ToInt32(row.RecordID);
            }
            using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
            {
                var myExamGroup = from c in db.ExamItemsGroup
                                  where c.ExamItemGroupID == examitemgroupid
                                  select c.GroupName;
                foreach (var group in myExamGroup)
                {
                    DropDownField3.Text = group.ToString();
                }
            }
        }

        protected void Add_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            if (DropDownField2.Text.ToString().Equals("请选择"))
            {
                X.Msg.Alert("提示信息", String.Format("请选择一个知识点")).Show();
            }
            else
            {
                if (DropDownField3.Text.ToString().Equals("请选择"))
                {
                    X.Msg.Alert("提示信息", String.Format("请选择一个题组")).Show();
                }
                else
                {
                    ExamAnalysis.Model.ExamItems newei = new ExamAnalysis.Model.ExamItems();
                    newei.ExamItemGroupID = examitemgroupid;
                    newei.KnowledgePointID = knowledgeid;
                    newei.ItemScore = Convert.ToInt32(ItemScore.Text);
                    newei.ItemName = ItemName.Text;
                    newei.ItemType = ItemType.Text;
                    newei.DifficultyLevel = Convert.ToInt32(DifficultyLevel.Text);
                    using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
                    {
                        db.ExamItems.InsertOnSubmit(newei);
                        db.SubmitChanges();
                        X.Msg.Alert("提示信息", String.Format("成功添加试题")).Show();
                        examitemgroupid = 0;
                        knowledgeid = 0;
                    }
                    Response.Redirect("ExamItemsInfo.aspx");
                }
            }
        }

        protected void Change_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
            {
                var newexamitem = from ei in db.ExamItems
                                  where ei.ExamItemID == examitemsid
                                  select ei;
                ExamAnalysis.Model.ExamItems newei = newexamitem.First();
                newei.ExamItemGroupID = examitemgroupid;
                newei.KnowledgePointID = knowledgeid;
                newei.ItemScore = Convert.ToInt32(ItemScore.Text);
                newei.ItemName = ItemName.Text;
                newei.ItemType = ItemType.Text;
                newei.DifficultyLevel = Convert.ToInt32(DifficultyLevel.Text);
                db.SubmitChanges();
                X.Msg.Alert("提示信息", String.Format("成功修改试题信息")).Show();
                examitemgroupid = 0;
                knowledgeid = 0;
            }
            Response.Redirect("ExamItemsInfo.aspx");
        }
    }
}